package com.lancoo.znbkxx.teacher.ui.dialog

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatDialog
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.databinding.DialogCommonBinding
import com.lancoo.znbkxx.teacher.ui.Presenter
import com.lancoo.znbkxx.teacher.utils.DensityUtils.dp2px
import com.lancoo.znbkxx.teacher.utils.DensityUtils.getScreenWidth

/**
 * Created by Dadong on 2019-12-23
 * Describe:
 */
class CommonDialog(private val mContext: Context, private val mActivity: Activity) : AppCompatDialog(mContext), Presenter {


    val message = ObservableField<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        setWidthAndHeight()
        window!!.setWindowAnimations(android.R.style.Animation_Dialog)
    }

    private fun setWidthAndHeight() {
        val lp = window!!.attributes
        lp.width = getScreenWidth(mActivity) - dp2px(mContext, 30f)
        window!!.attributes = lp     //设置生效
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.confirm_tv -> {
                dismiss()
                mConfirmListener.invoke()
            }
        }
    }


    override fun onCancel(view: View?) {
    }

    private fun renderView() {
        mBinding.contentTv.text = message.get()
    }

    override fun show() {
        renderView()
        super.show()
    }

    val mBinding: DialogCommonBinding by lazy {
        DataBindingUtil.inflate<DialogCommonBinding>(
                LayoutInflater.from(mContext), R.layout.dialog_common, null, false)
    }

    init {
        setContentView(mBinding.root)
        mBinding.presenter = this
    }

    private lateinit var mConfirmListener: () -> Unit
    fun setConfirmListener(listener: () -> Unit): CommonDialog {
        this.mConfirmListener = listener
        return this@CommonDialog
    }
}